package com.android.lovely.ui.dialog

import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.android.lovely.R
import com.android.lovely.databinding.DialogChatUserBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.room.BotCacheService
import com.android.lovely.room.BotCacheService.observeBot
import com.android.lovely.ui.chat.ChatMessageActivity
import com.android.lovely.ui.viewmodel.LoginViewModel
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/9 17:08
 */
class ChatProfileDialog : BaseDialogFragment<DialogChatUserBinding>() {

    val viewModel by activityViewModels<LoginViewModel>()

    private val randomNames by lazy {
        // 从资源文件中获取随机名称列表
        resources.getStringArray(R.array.random_names).toList()
    }


    private var canCancel: Boolean = false

    private var botId: Int = -1

    override fun initializeViews() {
        canCancel = arguments?.getBoolean("canCancel") ?: false
        botId = arguments?.getInt(ChatMessageActivity.CHAT_ID, 0) ?: 0
        lifecycleScope.observeBot(botId) {
            binding.nickname.setText(it.chatName)
            binding.male.isSelected = it.chatGender == 1
            binding.female.isSelected = it.chatGender == 2
            binding.unknown.isSelected = it.chatGender != 1 && it.chatGender != 2
            checkEnable()
            binding.content.setText(it.chatDec)

        }
        binding.close.clickable {
            dismissAllowingStateLoss()

        }
        binding.nickname.doAfterTextChanged {
            checkEnable()
        }

        if (binding.nickname.text.isNullOrEmpty()) {
            setRandomNickname()
        }
        binding.ivRole.clickable {
            setRandomNickname()
        }
        binding.save.clickable {
            if (binding.nickname.text.isNullOrBlank()) {
                return@clickable
            }
            update()
        }
        binding.male.clickable {
            binding.male.isSelected = true
            binding.female.isSelected = false
            binding.unknown.isSelected = false
            checkEnable()
        }
        binding.female.clickable {
            binding.male.isSelected = false
            binding.female.isSelected = true
            binding.unknown.isSelected = false
            checkEnable()
        }
        binding.unknown.clickable {
            binding.male.isSelected = false
            binding.female.isSelected = false
            binding.unknown.isSelected = true
            checkEnable()
        }
    }

    private fun setRandomNickname() {
        binding.nickname.setText(randomNames.random())
    }

    private fun checkEnable() {
        binding.save.isEnabled = !binding.nickname.text.isNullOrBlank()
    }

    private fun update() {
        val currentName = binding.nickname.text.toString()
        val gender = when {
            binding.male.isSelected -> 1
            binding.female.isSelected -> 2
            binding.unknown.isSelected -> 0
            else -> 1 // 默认值，可根据实际情况调整
        }
        viewModel.saveProfile(botId, currentName, gender, binding.content.text.toString()) {
            BotCacheService.updateBot(botId) {
                chatName = currentName
                chatGender = gender
                chatDec = binding.content.text?.toString() ?: chatDec
            }
            dismissAllowingStateLoss()
        }
    }


    override fun isBottom(): Boolean {
        return true
    }
}